package pro.shuangxi.model.book.controller;

import mybatisplus.pojo.Book;
import mybatisplus.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author ：mengshx
 * @date ：Created in 2020/12/12 19:30
 * @description：小说上传
 */
@Controller
public class BookUploadController {
    @Autowired
    IBookService bookService;
    @RequestMapping("uploadBook.do")
    @ResponseBody
    public Map bookUpload(@RequestParam("upload") MultipartFile multipartFile,@RequestParam("accountid") Integer accountid) throws IOException {
        File file = new File("/pro/shuangxi/books");
        if (!file.exists()) {
            file.mkdirs();
        }
        File save = new File(file, UUID.randomUUID().toString() + ".txt");
        multipartFile.transferTo(save);
        HashMap<Object, Object> data = new HashMap<>();
        data.put("message", "文件上传成功了");
        data.put("file", multipartFile.getName());
        Book book = new Book();
        book.setPath(save.getAbsolutePath());
        book.setName(multipartFile.getOriginalFilename());
        book.setOwner(accountid);
        bookService.save(book);
        return data;

    }
}
